package com.three.sso.mobile.service;


import com.three.core.entity.FrontUser;
import com.three.core.vo.R;
import com.three.sso.mobile.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


//sso不负责操作数据库 所有对数据库的操作交于services
@Service
public class MyUserDetailService implements UserDetailsService {
    @Autowired
    private UserFeign userFeign;
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        //根据账号查询用户信息
        R<FrontUser> r = userFeign.getCustomerByName(userName);
        if(r.getData()==null){
            throw new UsernameNotFoundException("用户未找到");
        }
        //获取用户信息
        FrontUser u = r.getData();

        List<SimpleGrantedAuthority> simpleGrantedAuthority = new ArrayList<>();
        User user = new User(u.getPhone(), u.getPassword(), simpleGrantedAuthority);

        return user;
    }
}
